package com.engine.zhlx.crm.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author dusongsong
 * @date 2024年12月17日 09:29
 */
public class CrmUtil extends BaseBean {
    /**
     * 建模权限重构
     *
     * @param userid
     * @param formmodeid
     * @param id
     */
    private void newModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }

    /**
     * 将数据插入表单建模
     *
     * @param map
     */
    public boolean insertModelData(String tablename, Map<String, Object> map) {
        int dataid = 0;
        int userid = 1;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        boolean result = false;
        //查询该模块表名
        try {
            rs.executeQuery("select a.id from modeinfo a left join workflow_bill b on a.formid=b.id where b.tablename='" + tablename + "'");
            rs.next();
            int modeid = Util.getIntValue(rs.getString("id"));
            String uuid = UUID.randomUUID().toString();
            String insSql = "insert into " + tablename
                    + "(modeuuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values" +
                    "('" + uuid + "','" + userid + "','" + TimeUtil.getCurrentDateString() + "','" + TimeUtil.getOnlyCurrentTimeString() + "','" + modeid + "')";
            if (rs.executeUpdate(insSql)) {
                rs1.executeQuery("select id from " + tablename + " where modeuuid='" + uuid + "'");
                rs1.next();
                dataid = Util.getIntValue(rs1.getString("id"));
                if (dataid > 0) {
                    //遍历数据 进行update
                    String updatesql = "update " + tablename + " set ";
                    Set<String> keySet = map.keySet();
                    for (String key : keySet) {
                        updatesql += key + "='" + map.get(key).toString() + "',";
                    }
                    if (updatesql.endsWith(",")) {
                        updatesql = updatesql.substring(0, updatesql.length() - 1);
                        updatesql += " where id=" + dataid;
                        writeLog(">>>>>insertModelData>>>>updatesql=" + updatesql);
                        if (!rs.executeUpdate(updatesql)) {
                            rs1.executeUpdate("delete from " + tablename + " where id=?", dataid);
                        } else {
                            result = true;
                        }
                    }
                    //进行权限重构
                    newModeRight(userid, modeid, dataid);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新数据
     *
     * @param tablename 表名
     * @param map       数据集合
     * @param dataid    更新用唯一标识
     * @return
     */
    public boolean updateModelData(String tablename, String dataid, Map<String, Object> map) {
        RecordSet rs = new RecordSet();
        String updatesql = "update " + tablename + " set  ";
        String valuesStr = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (valuesStr.equals("")) {
                valuesStr = key + "='" + map.get(key) + "'";
            } else {
                valuesStr += "," + key + "='" + map.get(key) + "'";
            }
        }
        updatesql += valuesStr + " where id='" + dataid + "'";
        writeLog(">>>>>updateModelData>>>>updatesql=" + updatesql);
        return rs.execute(updatesql);
    }

    /**
     * 判断数据是否存在
     *
     * @param value
     * @param tableName
     * @return
     */
    public String isExistData(String value, String tableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + tableName + " where sjbm=?", value);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public boolean isJSONArray(String str) {
        boolean flag = false;
        if (!(str.trim().equals(""))) {
            try {
                JSONArray.parseArray(str);
                flag = true;
            } catch (Exception e) {
                flag = false;
            }
        }
        return flag;
    }

    public boolean isJSONObject(String str) {
        boolean flag = false;
        if (!(str.trim().equals(""))) {
            try {
                JSONObject.parseObject(str);
                flag = true;
            } catch (Exception e) {
                flag = false;
            }
        }
        return flag;
    }
}
